// pages/contributionVal/index.js
var app = getApp();
var util = require("../../utils/util.js");
var hostUrl = require("../../host.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    total_flag: true, //控制描述显示
    total: '',         //控制显示描述标题
    text: '',          //控制描述文字
    background_color: "#288CCB",
    
    icon_text:"",
    avatarUrl: '',
    nickName: '',
    buttonText: "下一步",
    bossName: "",
    shareDistribution: 0, //当前页
    nextPage: "/pages/shareDistribution/index",

    have_calculation: 0,

    color: ["#7bb5d5", "#feb45f", "#b2df62", "#b8bae0", "#9ccfc8"],
    shareholders_contribution_valuation_list:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getStorageSync('userInfo')) {
      this.setData({
        avatarUrl: JSON.parse(wx.getStorageSync('userInfo')).avatarUrl,
        nickName: JSON.parse(wx.getStorageSync('userInfo')).nickName,
      });
    }

    //得到缓存的所有股东
    var shareholders_list = app.globalData.shareholders_list;
    //得到当前bossid
    var shareDistribution = app.globalData.shareDistribution;

    //如果当前是最后一个boss则修改跳转路径 转到 是否查看结果
    if (shareDistribution == shareholders_list.length - 1) {

      this.setData({
        buttonText: "查看计算结果",
        nextPage: "/pages/pie/index",
        background_color: "#32CD32",
      });
    }

    var bosstName = shareholders_list[Number(shareDistribution)].shareholders_name;
    this.setData({
      have_calculation: app.globalData.have_calculation,
      bossName: bosstName,
      shareDistribution: shareDistribution,
      shareholders_contribution_valuation_list:app.globalData.shareholders_contribution_valuation_list
    });
    if (app.globalData.have_calculation == 1) {
      this.setData({
        icon_text: "报告记录"
      })
    } else {
      this.setData({
        icon_text: "返回首页"
      })
    }

    this.$wuxToptips = app.Wux().$wuxToptips;
  },

  //路由
  submitForm: function (e) {
    var that = this;
    //当前股东id
    var shareDistribution = Number(this.data.shareDistribution);
    //用来存入 缓存数组
    var shareholders_contribution_valuation_list = {};

    //得到提交数据
    var data = e.detail.value;
    for (var key in data) {
      data[key] = e.detail.value[key] == "" ? 0 : data[key];
    }
    
    //将缓存中的数据替换
    app.globalData.shareholders_contribution_valuation_list[shareDistribution + 1] = e.detail.value;

    //当前页加一
    app.globalData.shareDistribution = shareDistribution + 1;

    if (!that.chackCount(app.globalData.shareholders_contribution_valuation_list)){

      var hideToptips = this.$wuxToptips.show({
        timer: 3000,
        text: '股东贡献估值大于公司估值',
        success: () => {}
      });
      return false;
    }
    //如果当前是最后一个boss则把所有数据存入 entity中
    if (shareDistribution == app.globalData.shareholders_list.length - 1) {

      var jstr = JSON.stringify(app.globalData.shareholders_contribution_valuation_list)
      app.globalData.entity.shareholders_contribution_valuation_list = jstr;

      //请求数据
      var url = hostUrl.add_calculator;
      var paramMd5 = app.paramsMd5(app.globalData.entity);
      wx.request({
        url: url,
        data: paramMd5,
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        dataType: 'json',
        success: function (response) {

          console.log(response);
          if (response.data.status) {
            wx.reLaunch({
              url: that.data.nextPage + "?record_id=" + response.data.msg,
              success: function () { },
              fail: function (e) {
                console.log(e);
              },
            });
          } else {
          
            var hideToptips = that.$wuxToptips.show({
              timer: 3000,
              text: response.data.msg,
              success: () => {}
            });
          }
        }
      })
    } else {

      wx.navigateTo({
        url: that.data.nextPage,
        success: function () { },
        fail: function (e) {
          console.log(e);
        },
      });
    }

  },

  //用来验证个人估值总数是否大于公司估值  传入公司估值 和当前估值数据列表
  chackCount: function (shareholders_contribution_valuation_list){
    //循环列表取出数据比对
    var count = 0;
    var company_valuation = app.globalData.entity.company_valuation
    
    for (var key in shareholders_contribution_valuation_list){
      var value = shareholders_contribution_valuation_list[key];
      for (var ki in value){
        count += Number(value[ki]);
      }
    }

    console.log("count:" + count);
    console.log(shareholders_contribution_valuation_list);
    if (count > company_valuation){
      return false;
    }
    return true;
  },
  navigateBack: function () {


    app.globalData.shareholders_contribution_valuation_list = this.data.shareholders_contribution_valuation_list

    wx.navigateBack({
    })
  },
  show: function (e) {

    var that = this;
    switch (e.currentTarget.id) {
      case "1":
        that.setData({
          total: '工作时间',
          text: app.globalData.contribution_valuation.working_time,
        });
        break;
      case "2":
        that.setData({
          total: '现金投入',
          text: app.globalData.contribution_valuation.cash_into,
        });
        break;
      case "3":
        that.setData({
          total: '办公场地',
          text: app.globalData.contribution_valuation.office_space,
        });
        break;
      case "4":
        that.setData({
          total: '实物资产',
          text: app.globalData.contribution_valuation.physical_assets,
        });
        break;
      case "5":
        that.setData({
          total: '知识产权',
          text: app.globalData.contribution_valuation.Intellectual_property,
        });
        break;
      case "6":
        that.setData({
          total: '其他资源',
          text: app.globalData.contribution_valuation.other_resources,
        });
        break;
      default:
        break;
    }
    that.setData({ total_flag: false })
  },
  //消失

  hide: function () {

    this.setData({ total_flag: true })
  },

  icon_redirectTo: function () {
    var that = this;
    if (that.data.have_calculation == 1) {
      wx.redirectTo({
        url: '/pages/history/index',
      })
    } else {
      wx.redirectTo({
        url: '/pages/index/index',
      });
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

    app.globalData.shareholders_contribution_valuation_list = this.data.shareholders_contribution_valuation_list
  },
});
